<?php

/**
 * talleres actions.
 *
 * @package    atenea
 * @subpackage talleres
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class talleresActions extends autotalleresActions
{
	  public function executeIndex()
  {
    return $this->forward('talleres', 'list');
  }

  public function executeList()
  {

    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/solicitud_taller/filters');

    // pager
    $this->pager = new sfPropelPager('SolicitudTaller', 20);
    $c = new Criteria();
    $c->add(SolicitudTallerPeer::USUARIO_ID,$this->getRequestParameter('filter[usuario_id]'));
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', 1));
    $this->pager->init();
  }

  public function executeCreate()
  {
    return $this->forward('talleres', 'edit');
  }

  public function executeSave()
  {
    return $this->forward('talleres', 'edit');
  }

  public function executeEdit()
  {
    $this->solicitud_taller = $this->getSolicitudTallerOrCreate();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updateSolicitudTallerFromRequest();

      $this->saveSolicitudTaller($this->solicitud_taller);

      $this->setFlash('notice', 'Your modifications have been saved');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('talleres/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('talleres/list');
      }
      else
      {
        return $this->redirect('talleres/edit?id='.$this->solicitud_taller->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  public function executeDelete()
  {
    $this->solicitud_taller = SolicitudTallerPeer::retrieveByPk($this->getRequestParameter('id'));
    $this->forward404Unless($this->solicitud_taller);

    try
    {
      $this->deleteSolicitudTaller($this->solicitud_taller);
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'No puede eliminar su inscripcion al taller seleccionado');
      return $this->forward('taller', 'edit');
    }

    return $this->redirect('taller/edit');
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->solicitud_taller = $this->getSolicitudTallerOrCreate();
    $this->updateSolicitudTallerFromRequest();

    $this->labels = $this->getLabels();

    return sfView::SUCCESS;
  }

  protected function saveSolicitudTaller($solicitud_taller)
  {
    $solicitud_taller->save();

  }

  protected function deleteSolicitudTaller($solicitud_taller)
  {
    $solicitud_taller->delete();
  }

  protected function updateSolicitudTallerFromRequest()
  {
    $solicitud_taller = $this->getRequestParameter('solicitud_taller');

    if (isset($solicitud_taller['usuario_id']))
    {
    $this->solicitud_taller->setUsuarioId($solicitud_taller['usuario_id'] ? $solicitud_taller['usuario_id'] : null);
    }
    if (isset($solicitud_taller['taller_id']))
    {
    $this->solicitud_taller->setTallerId($solicitud_taller['taller_id'] ? $solicitud_taller['taller_id'] : null);
    }
  }

  protected function getSolicitudTallerOrCreate($id = 'id')
  {
    if (!$this->getRequestParameter($id))
    {
      $solicitud_taller = new SolicitudTaller();
    }
    else
    {
      $solicitud_taller = SolicitudTallerPeer::retrieveByPk($this->getRequestParameter($id));

      $this->forward404Unless($solicitud_taller);
    }

    return $solicitud_taller;
  }

  protected function processFilters()
  {
  	echo $this->getRequestParameter('filters');
    if ($this->getRequest()->hasParameter('filter'))
    {

      $filters = $this->getRequestParameter('filters');

      $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/solicitud_taller/filters');
      $this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/solicitud_taller/filters');
    }
  }

  protected function processSort()
  {
    if ($this->getRequestParameter('sort'))
    {
      $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/solicitud_taller/sort');
      $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/solicitud_taller/sort');
    }

    if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/solicitud_taller/sort'))
    {
    }
  }

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['usuario_id_is_empty']))
    {
      $criterion = $c->getNewCriterion(SolicitudTallerPeer::USUARIO_ID, '');
      $criterion->addOr($c->getNewCriterion(SolicitudTallerPeer::USUARIO_ID, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['usuario_id']) && $this->filters['usuario_id'] !== '')
    {
      $c->add(SolicitudTallerPeer::USUARIO_ID, $this->filters['usuario_id']);
    }
  }

  protected function addSortCriteria($c)
  {
    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/solicitud_taller/sort'))
    {
      $sort_column = SolicitudTallerPeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
      if ($this->getUser()->getAttribute('type', null, 'sf_admin/solicitud_taller/sort') == 'asc')
      {
        $c->addAscendingOrderByColumn($sort_column);
      }
      else
      {
        $c->addDescendingOrderByColumn($sort_column);
      }
    }
  }

  protected function getLabels()
  {
    return array(
      'solicitud_taller{id}' => 'Id:',
      'solicitud_taller{usuario_id}' => 'Usuario:',
      'solicitud_taller{taller_id}' => 'Taller:',
    );
  }
}
